import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

/**
 * NormalGameScreen Class
 * This is a subclass of WordGameScreen, simply used to show
 * a particular game in which the player scores points in a 
 * time limit
 * 
 * @author Zong Xiong
 */
public class NormalGameScreen extends WordGameScreen {

/***************************************************
 * Fields
 ***************************************************/
	
	// time limit the player has to score points in
	private long TIME_LIMIT= 30000;
	// used to load and store scores
	private Preferences prefs;
	// tracks score placement after goal is met
	private int scorePlacement = 0;
	

/***************************************************
 * Public Methods
 ***************************************************/
	
	/**
	 * Default Constructor
	 * calls superclass constructor then own initialization
	 */	
	public NormalGameScreen() {
		super();
		normalGameInitialize();
	}
	
	/**
	 * Constructor with parent Game
	 * calls superclass constructor then own initialization
	 * @param inGame , parent game
	 */
	public NormalGameScreen(Game inGame) {
		super();
		normalGameInitialize();
	}
	
/***************************************************
 * Protected Methods
 ***************************************************/
	
	/**
	 * Modified draw method
	 * Adds a line of text for score display
	 */
	protected void draw() {
		super.draw();
		printBoard("Time:" + gameTimer.printTime() + "  Points:" + pointScore , 20, 79);
	}
	
	/**
	 * Modified update method
	 * Tracks if goals are met for game and if so ends game
	 */	
	protected void update() {
		super.update();
		
		// checks if timer has run out, if so we end the game
		// and check if the player has charted in scores
		if (gameTimer.getTime() == 0) {
			super.loseGame();
			
			// checks if the player charted
			for (int i=1; i<6 && scorePlacement == 0; i++) {
				if (pointScore > prefs.getLong("Place" + i + "Score")) {
					scorePlacement = i;
					shiftScores(i);
					prefs.putLong("Place" + i + "Score", pointScore);
				}
			}
			// if player has charted, we prompt for a name
			if (scorePlacement > 0) {
				super.enterHighScore();
				prefs.putString("Place" + scorePlacement + "Name", super.getScoreName() );
				prefs.flush();
			}
		}
	}
	
/***************************************************
 * Private Methods
 ***************************************************/
	
	/**
	 * ShiftScores
	 * shifts scores downward from scorePlacement
	 * @param scorePlacement, place acquired by player
	 */	
	private void shiftScores(int scorePlacement) {
		for (int i = 5; i > scorePlacement; i--) {
			prefs.putLong("Place" + (i) + "Score", prefs.getLong("Place" + (i-1) + "Score"));
			prefs.putString("Place" + (i) + "Name", prefs.getString("Place" + (i-1) + "Name"));
		}
	}
	
	/**
	 * normalGameInitialize
	 * initialization for a normal game
	 */
	private void normalGameInitialize() {
		// set timer
		gameTimer.setTime(TIME_LIMIT);
		gameTimer.setCountUp(false);
		// load score board
		prefs = Gdx.app.getPreferences("NormalWordGame");
	}
}
